//
//  menuViewController.m
//  movie
//
//  Created by 白浩洋 on 2018/4/24.
//  Copyright © 2018年 白浩洋. All rights reserved.
//

#import "menuViewController.h"
#import "HotSearchCollectionViewCell.h"
#import "HotHeadFreeCollectionReusableView.h"
#import "detailWebViewController.h"
@interface menuViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)UICollectionViewFlowLayout *flowLayout;
@property(nonatomic,strong)NSMutableArray *dataSource;
@end

@implementation menuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.collectionView.mj_header beginRefreshing];
}
- (void)requestData{
    [HYBNetworking postWithUrl:@"http://zx.ixinjiapo28.com/subscribe/userModules?appid=1021&uid=" refreshCache:YES params:nil success:^(id response) {
        NSLog(@"%@",response);
        NSDictionary *dic = [response objectForKey:@"resMsg"];
        NSArray *arr = [dic objectForKey:@"user_modules"];
        [self.dataSource removeAllObjects];
        [self.dataSource addObjectsFromArray:arr];
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView reloadData];
    } fail:^(NSError *error) {
        
    }];
}
-(NSMutableArray *)dataSource{
    if(!_dataSource){
        _dataSource = [[NSMutableArray alloc]init];
    }
    return _dataSource;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    detailWebViewController *dic = [[detailWebViewController alloc]init];
    dic.dataDic = self.dataSource[indexPath.item];
    dic.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:dic animated:YES];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HotSearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"hotSearchCell1" forIndexPath:indexPath];
    if(!cell){
        cell = [[HotSearchCollectionViewCell alloc] init];
    }
    NSDictionary *model=self.dataSource[indexPath.item];
    NSString *movieImage=model[@"icon2"];
    cell.movieName.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.3];
    cell.movieName.text = model[@"name"];
    [cell.movieImage sd_setImageWithURL:[NSURL URLWithString:movieImage] placeholderImage:[UIImage imageNamed:@"logo"]];
    cell.cellLabel.text = @"";
    cell.readLab.text = @"";
    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(ScreenWidth / 4 - PXChange(30), (ScreenWidth / 4.0f));
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(20,20,10,20);
}
-(UICollectionViewFlowLayout *)flowLayout{
    if(!_flowLayout){
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        //设置滑动方向
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _flowLayout.minimumLineSpacing = PXChange(20);
    }
    return _flowLayout;
}
-(UICollectionView *)collectionView{
    if(!_collectionView){
        _collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth,ScreenHeight-64 - 49) collectionViewLayout:self.flowLayout];
        _collectionView.delegate=self;
        _collectionView.dataSource=self;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self requestData];
        }];
        _collectionView.contentSize=CGSizeMake(ScreenWidth/2.0f, PXChange(500));
        [_collectionView registerNib:[UINib nibWithNibName:@"HotSearchCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"hotSearchCell1"];
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
